<?php
/**
 * Installer modules installation
 *
 * @copyright		The XOOPS project http://www.xoops.org/
 * @license			http://www.fsf.org/copyleft/gpl.html GNU General Public License (GPL)
 * @package			installer
 * @since			2.0.18
 * @version			$Id: page_moduleinstaller.php 598 2006-06-30 15:45:20Z skalpa $
 *
 * @author			Haruki Setoyama  <haruki@planewave.org>
 * @author			Kazumi Ono <webmaster@myweb.ne.jp>
 * @author			Skalpa Keo <skalpa@xoops.org>
 * @author			dugris <dugris@frxoops.org>
**/

require_once 'common.inc.php';
if ( !defined('XOOPS_INSTALL') ) { die('XOOPS Custom Installation die'); }

$wizard->setPage( 'moduleinstaller' );
$wizard->loadModuleLangFile();

require_once XOOPS_ROOT_PATH.'/class/database/databasefactory.php';
$xoopsDB =& XoopsDatabaseFactory::getDatabaseConnection();
require_once '../kernel/object.php';
require_once '../class/criteria.php';
require_once '../include/functions.php';
require_once '../class/xoopsformloader.php';
require_once '../class/xoopslists.php';
require_once '../class/module.textsanitizer.php';

$pageHasForm = true;
$pageHasHelp = false;

if ( $_SERVER['REQUEST_METHOD'] == 'POST' ) {
	include_once '../class/xoopsblock.php';
	include_once '../kernel/module.php';
	include_once '../include/cp_functions.php';
   include_once '../include/version.php';
	include_once './include/modulesadmin.php';

	$config_handler =& xoops_gethandler('config');
	$xoopsConfig =& $config_handler->getConfigsByCat(XOOPS_CONF);

	$msgs = array();
	foreach ($_REQUEST['modules'] as $dirname => $installmod) {
		if ($installmod) {
			$msgs[] = xoops_module_install($dirname);
		}
	}

	$pageHasForm = false;

   if ( count($msgs) > 0 ) {
		$content = "<div class='x2-note successMsg'>" . INSTALLED_MODULES . "</div><ul class='log'>";
		foreach ( $msgs as $msg ) {
			$content .= "<dt>$msg</dt>";
		}
		$content .= "</ul>";
   } else {
   	$content = "<div class='x2-note confirmMsg'>" . NO_INSTALLED_MODULES . "</div>";
   }

	$filename = XOOPS_CACHE_PATH.'/adminmenu.php';
   if ( file_exists($filename) ) {
   	@unlink( $filename );
   }
} else {
	if ( $wizard->trust_path ) {
      array_push($wizard->modules, "protector");
	}

	// Get installed modules
	$module_handler =& xoops_gethandler('module');
	$installed_mods =& $module_handler->getObjects();
	$listed_mods = array();
	foreach ( $installed_mods as $module ) {
		$listed_mods[] = $module->getVar('dirname');
	}

	$dirlist = XoopsLists::getModulesList();
	$toinstal = 0;

	$javascript = "";
	$content = "<ul class='log-module'>";
	foreach($dirlist as $file){
		clearstatcache();
		if ( !in_array($file, $listed_mods) ) {
			$value = 0;
			$style = "";
			if ( in_array($file, $wizard->modules) ) {
				$value = 1;
				$style = " style='background-color:#E6EFC2;'";
			}

			$file = trim($file);

			$module =& $module_handler->create();
			$module->loadInfo($file);

			$form = new XoopsThemeForm('', 'modules', 'index.php', 'post');
			$moduleYN = new XoopsFormRadioYN('', 'modules['. $module->getInfo('dirname') . ']', $value, _YES, _NO);
			$moduleYN->setExtra( "onclick='selectModule(\"" . $file . "\", this)'" );
			$form->addElement($moduleYN);

			$content .= "<table id='" . $file . "'" . $style . ">\n";
			$content .= "<tr>\n";
			$content .= "	<td class='module_col1'><img src='" . XOOPS_URL . "/modules/" . $module->getInfo('dirname') . "/" . $module->getInfo('image') . "' alt='" . htmlspecialchars($module->getInfo('name')) . "'/></td>\n";
			$content .= "	<td class='txtleft'><span>";
			$content .= "		" . htmlspecialchars($module->getInfo('name')) . "&nbsp;" . number_format( round($module->getInfo('version'), 2), 2) . "&nbsp;(" . $module->getInfo('dirname') . ")";
			$content .= "		</span><br />" .  htmlspecialchars($module->getInfo('description'));
			$content .= "	</td>\n";
			$content .= "	<td class='module_col1'>";
			$content .= $moduleYN->render() ;
			$content .= "	</td></tr>\n";
			$content .= "</table>";
			$toinstal++;
		}
	}
	$content .= "</ul><script type='text/javascript'>" . $javascript . "</script>";
	if ( $toinstal == 0 ) {
		$pageHasForm = false;
		$content = "<div class='x2-note confirmMsg'>" . NO_MODULES_FOUND . "</div>";
	}
}

include 'install_tpl.php';

?>